﻿/**
 * ...
 * @author sminutoli
 */
import com.dmotiko.prune.*
import com.dmotiko.general.*
import mx.transitions.easing.Regular;
import mx.transitions.easing.Strong;
import mx.transitions.Tween;
import mx.utils.Delegate;
 
class SiteBar extends BaseClip
{
	
	private var btn_active:ButtonClip;
	private var btns:Array;
	private var container:MovieClip;
	private var bShowing:Boolean;
	private var sLink:String;
	private var nSpace:Number;
	
	private function initClip():Void {
		sLink = "MainBtn";
		nSpace = 0;
		bShowed = false;
		Site.getApp().addEventListener( WebSite.SECTION_CHANGED, Delegate.create(this, section_changed ) );
	}
	
	private function section_changed(e):Void {
		
	}
	
	private function getBtnByValue( value ):ButtonClip
	{
		for (var i:Number = 0; i < btns.length; i++) 
		{
			if ( btns[i].getData().value == value ) return btns[i];
		}
	}
	
	private function refreshData():Void {
		//cuando me refrescan la data hago mierda todo
		if ( !container ) container = createEmptyMovieClip( "container", this.getNextHighestDepth() );
		else container = createEmptyMovieClip( "container", container.getDepth() );
		btns = new Array();
		
		createBtns();
		
	}
	
	private function createBtns(initFirst:Boolean):Void
	{
		//creo los botones
		for (var i:Number = 0; i < data.length; i++){
			var clip:ButtonClip = ButtonClip( container.attachMovie( sLink, "mcBtn" + i, container.getNextHighestDepth() ) );
			clip._visible = false;
			clip._alpha = 0;
			clip.setData( data[i] );
			if ( i == 0 && initFirst != false ) {
				
				btn_active = clip;
				clip.gotoAndPlay( ButtonClip.NORMAL );
				clip.setActive( true, true );
			}
			clip.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, btn_changed ) );
			btns.push( clip );
		}
	}
	
	public function show():Void
	{
		
		bShowing = true;
		
		var nY:Number = 0;
		for ( var i = 0; i < btns.length; i++){
			btns[i]._visible = true;
			var bCheck:Boolean =  i == btns.length -1;
			registerTween( "fadeBtn" + i, new Tween( btns[i], "_alpha", Regular.easeOut, btns[i]._alpha, 100, 0.7, true), false, bCheck  );
			registerTween( "moveBtn" + i, new Tween( btns[i], "_y", Strong.easeOut, btns[i]._y, nY, 1, true) );
			nY += btns[i]._height + nSpace;
		}
		
	}
	
	public function hide():Void
	{
		bShowing = true;
		
		for ( var i = 0; i < btns.length; i++){
			btns[i]._visible = true;
			var bCheck:Boolean =  i == btns.length -1;
			registerTween( "fadeBtn" + i, new Tween( btns[i], "_alpha", Regular.easeOut, btns[i]._alpha, 0, 0.7, true), false, bCheck  );
			registerTween( "moveBtn" + i, new Tween( btns[i], "_y", Strong.easeInOut, btns[i]._y, 0, 1, true) );
		}
		
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		if ( key.indexOf("fadeBtn") > -1 && btns[0]._alpha > 0 ) {
			bShowing = false;
			showEnd();
						
		} else if ( key.indexOf("fadeBtn") > -1 && btns[0]._alpha == 0 ) {
			bShowing = false;
			for ( var btn in btns ) { btn._visible = false };			
			hideEnd();
		}
	}
	
	private function btn_changed( e ):Void 
	{
		var btn = e.target;
		if ( btn && btn != btn_active ) {
			btn_active.setActive( false, true );
			btn_active = btn;
			if ( btn_active._currentframe == 1 ) btn_active.onRollOver();
			btn_active.setActive( true, true );
		}
		
	}
	
}